package com.loop.unclelend.network

import com.loop.unclelend.system.analysis.AnalyseBuilder
import com.loop.unclelend.system.analysis.AnalyseUtil
import com.loop.unclelend.utils.helper.SharedHelper
import org.json.JSONObject
import java.util.Random

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/2 10:59
 * @Annotation：接口API
 */
object ApiService {

    /**
     * https://www.ebclwebsite.net
     * 域名
     */
    const val UNCLE_BASE_URL = "https://www.ebclwebsite.net"

    /**
     * 接口Url
     */
    object API_URL{
        /**
         * 地区转发
         * /v1/timezonePath
         */
        const val UNCLE_API_TIMEZONE_URL = "/xb0kjDTET2tHHa3oI3Y4Hw"
        /**
         * APP业务统一请求接口
         * /v1/commonApi
         */
        const val UNCLE_API_COMMON_URL = "/wT5HXwF0e9FAYt2pzgjuwg"
        /**
         * APP业务统一请求接口（短信）
         * /v1/commonApi
         */
        const val UNCLE_API_SMS_URL = "/wT5HXwF0e9FAYt2pzgjuwg"
        /**
         * APP业务统一请求接口（登录）
         * /v1/commonApi
         */
        const val UNCLE_API_LOGIN_URL = "/wT5HXwF0e9FAYt2pzgjuwg"
        /**
         * 图片上传
         * /v1/imageUpload
         */
        const val UNCLE_API_UPLOAD_FILE_URL = "/PKa3n34lVTq9DAr7eVrjg"
    }

    /**
     * 数据分析Path
     */
    object ANALYSIS_PATH{

        /**
         * googel install 请求路径
         * /v1/channelInstallTemp
         */
        const val UNCLE_ANALYSIS_GOOGLE_INSTALL_PATH = "/HOCz4K8pDwrsW6Ud1ifXyQ"

        /**
         * adjust install 请求路径
         * /v1/adjustInstallTemp
         */
        const val UNCLE_ANALYSIS_ADJUST_INSTALL_PATH = "/zhLrtyLwYwQio3FLkAQhA"

        /**
         * facebook install 请求路径
         * /v1/saveFbAppData
         */
        const val UNCLE_ANALYSIS_FACEBOOK_INSTALL_PATH = "/ozkXTjbvxACWFaloX5DuyQ"

        /**
         * AppList 请求路径
         * /v2/userAppList
         */
        const val UNCLE_ANALYSIS_APPLIST_PATH = "/r2sTbs3xmgx3CaChP9yq1Ig"

        /**
         * 请求路径
         * /v2/smsRecord
         */
        const val UNCLE_ANALYSIS_SMS_PATH = "/TYQHJmkljfG5dK9xJLgzA"

        /**
         * device 请求路径
         * /v1/deviceInfo
         */
        const val UNCLE_ANALYSIS_DEVICE_PATH = "/OJyRLdobUWrEqCQI9zBuA"

        /**
         * battery 请求路径
         * /v1/userDeviceBattery
         */
        const val UNCLE_ANALYSIS_BATTERY_PATH = "/kfEesHBAfMwz7rwlNbJUg"

        /**
         * hardwave 请求路径
         * /v1/userDeviceHardware
         */
        const val UNCLE_ANALYSIS_HARDWAVE_PATH = "/jHA0v4IeiYD9opwgul2XcA"

        /**
         * contact 请求路径
         * /v2/phoneBookList
         */
        const val UNCLE_ANALYSIS_CONTACT_PATH = "/YoA5DCZzLu6HRE7pHq84A"

        /**
         * storge 请求路径
         * /v1/userDeviceStorage
         */
        const val UNCLE_ANALYSIS_STORGE_PATH = "/r7Pti6b3lIjKswds3L7XVA"

        /**
         * wifi 请求路径
         * /v1/userDeviceWifi
         */
        const val UNCLE_ANALYSIS_WIFI_PATH = "/TEx6EacPxuhBQJ6ixcZRTA"

        /**
         * calllog 请求路径
         * /v2/callRecord
         */
        const val UNCLE_ANALYSIS_CALLLOG_PATH = "/oyMdcMGDxKdLNgtzhJfJw"
    }


    /**
     * 接口Path
     */
    object API_PATH{
        /**
         * APP配置参数 Path
         * /v1/getAppShowInfo
         */
        const val UNCLE_API_APP_INFO_PATH = "oULggv1j8lgywcUr6h3uw"

        /**
         * 发送验证码
         * /v1/smsCode
         */
        const val UNCLE_API_SEND_SMS_PATH = "PZpDIb1Girnj2pvRp6ILA"

        /**
         * 登录
         * /v1/userLogin
         */
        const val UNCLE_API_LOGIN_PATH = "LNIntZGvTfdTo3I1DEabw"

        /**
         * 用户流程状态
         * /v1/userProcess
         */
        const val UNCLE_API_PROCESS_PATH = "hWQkFUTAKFl1NvibBc9vQ"

        /**
         * 保存用户图片信息（身份证、护照、驾驶证、人脸）
         * /v1/ocrInfo
         */
        const val UNCLE_API_OCR_INFO_PATH = "git0v1SgAMG6lj8KxXOsw"

        /**
         * 用户贷款额度
         * /v1/loanLimit
         */
        const val UNCLE_API_LIMIT_PATH = "PLkOp9WkQSdpM2DHd76tag"

        /**
         * 用户贷款额度信息（循环额度）
         * /v1/getLoanHomePage
         */
        const val UNCLE_API_LOAN_LIMIT_PATH = "msVQctE2xB3krLGnZbhCNA"

        /**
         * 新用户贷款额度预览信息
         * /v1/getLoanApplyDetail
         */
        const val UNCLE_API_LOAN_APPLY_PATH = "r2gS9NT0qgfHcw3wlUj7HmA"

        /**
         * 可选额度和期限的预览信息(老用户)
         * /getLoanDetailPageForDays
         */
        const val UNCLE_API_LOAN_APPLY_CUSTOM_LIMIT_PATH = "Anw2sgJwitvUQ5p5RPBBQ"

        /**
         * 创建贷款申请订单
         * /v1/loanApply
         */
        const val UNCLE_API_LOAN_APPLY_ORDER_CREATE_PATH = "h4fZrExxhv3zSXtCpPBjtw"

        /**
         * 订单列表
         * /v1/getAllOrderList
         */
        const val UNCLE_API_LOAN_ORDER_PATH = "r96Uss8CzUWasg9SqFoswNw"

        /**
         * 贷款还款信息
         * /v1/repayInfo
         */
        const val UNCLE_API_LOAN_REPAYMENT_INFO_PATH = "ScAblnycEM8qfXABI2JQ9w"

        /**
         * 贷款还款链接
         * /v1/orderRepayH5
         */
        const val UNCLE_API_LOAN_REPAYMENT_URL_PATH = "svREQOMprW0KcOIjpsvJA"

        /**
         * 贷款还款链接（肯尼亚、乌干达）
         * /v1/getOrderRepayH5
         */
        const val UNCLE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH = "elVzJGKmTQe073SIi8hng"

        /**
         * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
         * /v1/queryPaymentStatus
         */
        const val UNCLE_API_LOAN_REPAYMENT_STATUS_PATH = "Jbe84Hi5NtfDR8kpiB8Ag"

        /**
         * 账户列表
         * /v1/getBindCardList
         */
        const val UNCLE_API_ACCOUNT_LIST_PATH = "r2cbdfi8DX7tuIpznwLIZ2A"

        /**
         * 已绑定账户列表（法语）
         * /v2/fetchBoundBankAccount
         */
        const val UNCLE_API_ACCOUNT_LIST_FRENCH_PATH = "CVmKaPeSLrO1nK1RBIAOg"

        /**
         * 添加账户
         * /v2/bindBankAccount
         */
        const val UNCLE_API_ACCOUNT_ADD_PATH = "eouHvEoXNTFwbkCLpOUiQ"

        /**
         * 删除已绑定的账户
         * /v1/deleteBoundBankAccount
         */
        const val UNCLE_API_ACCOUNT_DELETE_PATH = "uMUOba23noTX3m0yLzOkfw"

        /**
         * 评价弹框
         * /v1/fetchStarWindowConfig
         */
        const val UNCLE_API_STAR_COMMENT_PATH = "r8u9TLDcMoYo4PGJPEDQg"

        /**
         * 提交意见反馈
         * /v1/feedback
         */
        const val UNCLE_API_FEEDBACK_SUBMIT_PATH = "r8Z5a2Z7wivDygunRmbk5nA"

        /**
         * 推荐APP列表
         * /v1/userDeniedDc
         */
        const val UNCLE_API_RECOMMEND_APP_LIST_PATH = "omJka6aA7ua5V3p3tNmQnw"
        /**
         * 事件埋点
         * /v1/userBuriedPoint
         */
        const val UNCLE_API_TRACK_EVENT_PATH = "ELXOCIEEanIZcExi0poPAQ"

        /**
         * 消息列表
         * /v1/fetchUserMessages
         */
        const val UNCLE_API_MESSAGE_LIST_PATH = "r7vwmpEWLiVzK3PWjk41bA"

        /**
         * 产品列表
         * /queryNewStyleProducts
         */
        const val UNCLE_API_LOAN_PRODUCT_LIST_PATH = "nw6u2AT3Bb6wZApXzGTyg"

        /**
         *  复制产品A信息 到 产品B (配置信息、进件信息)
         *  /oneKeyCopyToApply
         */
        const val UNCLE_API_LOAN_PRODUCT_COPY_PATH = "eccvGPbItvmkJpy0okAMtA"

        /**
         * 优惠券列表
         * /v1/getUserCouponAndScoreInfo
         */
        const val UNCLE_API_COUPON_LIST_PATH = "r52rrHil5587XJqAuPRz8Yw"

        /**
         * 签到信息
         * /v1/userSign
         */
        const val UNCLE_API_SIGN_PATH = "r97U9sJNKhsgneRU6c5VcQ"

        /**
         *  存取积分、优惠券兑换
         *  /v1/changeUserScore
         */
        const val UNCLE_API_SAVE_OR_USE_PATH = "ekhPQdinSiXpl5hIR7pCg"

        /**
         * 保存自定义参数接口
         * /v1/saveAbUserInfo
         */
        const val UNCLE_API_SAVE_CUSTOM_PARAMS_PATH = "QKCmEsyJq67mpYWOvj4cA"

        /**
         * 获取自定义参数
         * /v1/getAbUserInfo
         */
        const val UNCLE_API_LOAD_CUSTOM_PARAMS_PATH = "HfbSaEsvKNb88IGEGsr5w"

    }

    /**
     * 获取请求Url
     */
    fun getHttpUrl(url: String): String{
        //地区转发
        val timeZone = SharedHelper.getUncleSPString(SharedHelper.UNCLE_SPKEY.UNCLE_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$UNCLE_BASE_URL/$timeZone$url"
        }
        return "$UNCLE_BASE_URL$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", AnalyseBuilder.product)                                           //  产品 APP名称
            put("token", SharedHelper.getUncleSPString(SharedHelper.UNCLE_SPKEY.UNCLE_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", SharedHelper.getUncleSPString(SharedHelper.UNCLE_SPKEY.UNCLE_USERID))                                             //  userId
            put("version", AnalyseBuilder.libVersion)                                        //  app版本号
            put("timp",System.currentTimeMillis())                                            //  时间戳
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this
    }

    /**
     * 添加随机参数增量 2
     */
    fun JSONObject.addRandomParams(): JSONObject{
        val mobile = SharedHelper.getUncleSPString(SharedHelper.UNCLE_SPKEY.UNCLE_MOBILE)
        val timeStamp = System.currentTimeMillis() / 1000
        apply {
            put("loginName",mobile)
            put("loginTimeStamp",timeStamp)
//            for (i in 0..7){
//                val addKey = getUncleRandomKey(1+Random().nextInt(4))
//                val addValue = getUncleRandomKey(1+Random().nextInt(16))
//                put(addKey.lowercase(),addValue.lowercase())
//            }
        }
        return this
    }

    /**
     * 随机字符串
     */
    private fun getUncleRandomKey(length: Int): String {
        val charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        val stringBuilder = StringBuilder()
        val random = Random()
        for (i in 0..length){
            val index = random.nextInt(charSet.length)
            stringBuilder.append(charSet[index]);
        }
        return stringBuilder.toString();
    }
}